package week_10;

public class NumberOf1Bits_191 {

    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            // n - 最低位1 二进制
            // lowbit n&-n
            n -= n & -n;
        }
        return count;
    }

    public int hammingWeight4(int n) {
        int res=0;
        while(n!=0){
            // 依次右移1位 判断是否为1
            if((n&1)==1){
                res +=1;
            }
            n = n>>1;
        }
        return res;
    }

    public int hammingWeight2(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & (1 << i)) != 0) count++;
        }
        System.out.println();
        return count;
    }

    public int hammingWeight3(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if (((n >> i) & 1) == 1) count++;
        }
        return count;
    }
}
